#!/usr/bin/env python3
#coding:utf-8

__author__ = 'xmxoxo<xmxoxo@qq.com>'


import pika
import sys
import time
import random
from config import *

print(rabbit_user, rabbit_pwd)
print(rabbit_ip,rabbit_port)

# 连接到MQ
credentials = pika.PlainCredentials(rabbit_user, rabbit_pwd)
parm = pika.ConnectionParameters(host=rabbit_ip, port=rabbit_port, virtual_host='/', credentials=credentials)

connection = pika.BlockingConnection(parm)

# 连接到频道
channel = connection.channel()
channel.exchange_declare(exchange='topic_exchange', exchange_type='topic', durable=True)


routing_key = sys.argv[1] if len(sys.argv) > 1 else 'fj.fz.230'

delay = 3
base = 18
areas = ['fz','xm','zz','np']
idx = 300

while 1:
    # 随机选择区域
    c_area = random.choice(areas)
    # 随机生成终端ID
    c_idx = random.randrange(idx)

    routing_key = 'fj.%s.%d' % (c_area, c_idx)
    base = base + random.random()-0.5
    message = str(round(base, 2))
    channel.basic_publish(exchange='topic_exchange',routing_key=routing_key,body=message)
    delays = delay + random.random()*10/5 - 2
    print(" [x] Sent %r:%r delay:%f" % (routing_key, message, delays))
    #print('delay:%f'%delays)
    time.sleep(delays)

connection.close()


if __name__ == '__main__':
    pass

